package com.vlyman.weixin.support;

import com.alibaba.fastjson.JSONObject;
import com.vlyman.weixin.utils.EmailYaml;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.util.List;
import java.util.Properties;

/**
 * 类描述：
 *   电子邮件发送类
 *  @author chenjs
 *  @date 2019/7/12 19:53
 **/
@Slf4j
public class MailSender {
    private MailEntity mail = new MailEntity();
    private static final Authenticator AUTHEN;
    private static final Properties PROPS;
    private static final List<String> RECEIVER_LIST;
    static {
        PROPS = new Properties();
        PROPS.setProperty("mail.smtp.auth", "true");
        PROPS.setProperty("mail.smtp.host", "smtp.qq.com");
        PROPS.setProperty("mail.smtp.port", "587");
        PROPS.setProperty("mail.user", EmailYaml.getValue("mail.user"));
        PROPS.setProperty("mail.password", EmailYaml.getValue("mail.password"));
        AUTHEN = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                String userName = EmailYaml.getValue("mail.user");
                String userPwd = EmailYaml.getValue("mail.password");
                return new PasswordAuthentication(userName, userPwd);
            }
        };
        RECEIVER_LIST = JSONObject.parseArray(EmailYaml.getValue("mail.receivers"), String.class);
    }

    public MailSender(String title, String content, MailContentTypeEnum typeEnum, List<String> targets) {
        this.mail.setTitle(title);
        this.mail.setContent(content);
        this.mail.setContentType(typeEnum.getValue());
        if (targets != null && !targets.isEmpty()){
            this.mail.setList(targets);
        } else {
            this.mail.setList(RECEIVER_LIST);
        }
    }

    public MailSender(String sender, String title, String content, MailContentTypeEnum typeEnum, List<String> targets) {
        this.mail.setNickName(sender);
        this.mail.setTitle(title);
        this.mail.setContent(content);
        this.mail.setContentType(typeEnum.getValue());
        if (targets != null && !targets.isEmpty()){
            this.mail.setList(targets);
        } else {
            this.mail.setList(RECEIVER_LIST);
        }
    }

    public void send(){
        try {
            Session mailSession = Session.getDefaultInstance(PROPS, AUTHEN);
            MimeMessage msg = new MimeMessage(mailSession);
            String nickName = MimeUtility.encodeText(
                    StringUtils.isNotBlank(mail.getNickName())? mail.getNickName() : EmailYaml.getValue("mail.nickName")
            );
            InternetAddress from = new InternetAddress(nickName+"<"+ EmailYaml.getValue("mail.user")+ ">");
            msg.setFrom(from);
            msg.setSubject(mail.getTitle());
            if (mail.getContentType().equals(MailContentTypeEnum.HTML.getValue())) {
                msg.setContent(mail.getContent(), mail.getContentType());
            } else if (mail.getContentType().equals(MailContentTypeEnum.TEXT.getValue())) {
                msg.setText(mail.getContent());
            }
            for (String address : mail.getList()) {
                InternetAddress to = new InternetAddress(address);
                msg.setRecipient(Message.RecipientType.TO, to);
                Transport.send(msg);
            }
        } catch (Exception e) {
            log.error("发送邮件失败！！", e);
        }
    }
}
